from locust import HttpLocust, TaskSet, task
import json.encoder

reqread = {
    "evtype": "1",
    "obtype": "MAN",
    "obid": "bbxadmin",
    "time_period": {
        "from": 1521711409,
        "to": 1521711509
    },
    "limit": {
        "offset": 0,
        "count": 50
    },
    "evcode": "1000"
}
readheader={
    "Content-Type": "application/x-www-form-urlencoded"
}

reqwrite = {
    "obtype": "MAN",
    "obid": "bbxadmin",
    "obname": "",
    "evdata": {
        "code": "1000",
        "subcode": "0",
        "object": {
            "type": "ORDER",
            "id": "152162581657975801657",
            "name": ""
        },
        "ext": {
            "app": "0",
            "appoint_time": "2018-03-21 17:49:00",
            "appoint_type": 0,
            "calc_type": 0,
            "car_class_id": "allcarclass",
            "car_count": 1,
            "car_level": 0,
            "car_type": 0,
            "car_type_name": "",
            "city": "",
            "combo_price": 0,
            "coupon_id": 0,
            "elements": [
                {
                    "count": 1,
                    "end": {
                        "address": "\u6f33\u5dde\u5e02 \u8297\u57ce\u533a\u5546\u4e1a\u57ce-\u516c\u4ea4\u8f66\u7ad9",
                        "location": {
                            "lat": 24.524771,
                            "lng": 117.677316
                        },
                        "name": ""
                    },
                    "is_mutiple_oncar": 0,
                    "start": {
                        "address": "\u53a6\u95e8\u5e02 \u540c\u5b89\u533a\u53a6\u95e8\u540c\u5b89\u5f71\u89c6\u57ce",
                        "location": {
                            "lat": 24.748658,
                            "lng": 118.171378
                        },
                        "name": "\u5148\u751f",
                        "need_sms": 1,
                        "phone": "18650180552"
                    }
                }
            ],
            "flight_no": "",
            "formula_price": 0,
            "line_id": "361000_to_363000",
            "oc_code": "00001",
            "order_message": "",
            "order_name": "bbxadmin",
            "order_origin": 1,
            "order_type": 0,
            "origin_type": 1,
            "phone": "18650180552",
            "price": 4500,
            "price_detail": {
                "base_discount": 100,
                "base_price": 4500,
                "price_id": "",
                "sale_amount": 0,
                "sale_id": 0,
                "sale_type": 0,
                "schedule_fee": 0
            },
            "seat_num": 0,
            "service_user": "bbxadmin",
            "tel": "18650180552",
            "time": "2018-03-21 17:50:16",
            "travel_type": 0,
            "uid": "4489586438647880260",
            "username": "bbxadmin"
        }
    }
}

class WriteApi(TaskSet):

    @task(10)
    def write(self):
        resp = self.client.post("/event/write", json.dumps(reqwrite))
        #print(resp.content)
        #print resp.content
class ReadApi(TaskSet):
    @task(10)
    def read(self):
        resp = self.client.request("POST","//event/read",None,True,headers=readheader,data=reqread,allow_redirects=False)
#        resp = self.client.post("//event/read", json.dumps(reqread))
        print(resp.text)
class WebsiteUser(HttpLocust):
    task_set = ReadApi
    min_wait = 30  #最小等待时间 ms
    max_wait = 100  #最大等待时间 ms

